import axios from 'axios';
// const baseUrl = process.env.VUE_APP_BASE_API
axios.defaults.baseURL = process.env.VUE_APP_BASE_API;
axios.defaults.timeout = 5000;
axios.defaults.headers['Content-Type'] = 'application/json; charset=utf-8';

export const request = (url, method, data, params) => {
    return new Promise((resolve, reject) => {
        axios({
            method: method,
            url: url,
            data: data,
            params: params
        }).then(res => {
            resolve(res);
        }).catch(err => {
            reject(err)
        })
    })
}
//请求拦截器
axios.interceptors.request.use(
    config => {
        // let token = localStorage.getItem('token') || '123'
        // config.headers.token = token
        return config
    },
    error => {  // 当发生错误时，执行该部分代码
        console.log(error); //调试用
        return Promise.reject(error)
    }
)
//相应拦截器
axios.interceptors.response.use(
    response => {
        if (response.data.code != 0) {
            return Promise.reject(response.data.message || 'error')
        }
        return response.data
    },
    error => {
        console.error(error)
        return Promise.reject(error) // 返回接口返回的错误信息
    })